<?php


namespace app\common\library;


use app\common\library\BaiDuMapModel;
use common;
use vtimer;

class UMaleModel
{

    var $agent_name = '多多e行';
    var $agent_id = 4;

    function code_to_msg($code)
    {

        $code_eror_msg = [
            1 => [1, '系统出错'],
            2 => [2, '无效参数'],
            0 => [0, 'OK'],
        ];

        if (isset($code_eror_msg[$code])) {
            return $code_eror_msg[$code];
        }
        return [1, '系统出错'];
    }

    //bike_enterprises
    function bike_enterprises()
    {

        $vt = new vtimer();
        $hs = [
            'Content-Type: application/json',
        ];

        $url = 'http://api.xiaoantech.com/api/v1/bike/enterprises';
        $data=[
            'enterpriseNo' => $this->agent_id,
            'enterpriseName' => $this->agent_name,
            'uscCode' => '',
            'registeredCapital' => '',
            'serviceAddress'=> '',
            'serviceContact' => '',
            'beName' => '',
            'beContact' => '',
            'updateTime' => date('Y-m-d H:i:s'),
        ];


        $result = common::curlpostjson_timeout($url, $data,10, $hs);
        if (empty($result)) {
            return [-1, "request error!", $vt->cost()];
        }

        if ($result['success'] == true) {
            return [$result['code'], $result['message'], $vt->cost()];
        }

        return [-2, $result['message'], $vt->cost()];
    }



    //bike_info
    function bike_Info($bike_info,$operate,$enableFlag=1)
    {

        $vt = new vtimer();
        $cords = (new BaiDuMapModel())->wgs_to_baidu($bike_info['coordinate']);

        $now = date('Y-m-d H:i:s');
        $url = 'http://221.7.240.130:8027/HSGPS/api/v1/bike/bikeInfo';
        $data=[
            'unitID' => $this->agent_id,
            'unitName' => $this->agent_name,
            'bikeNo' => $bike_info['sn'],
            'lockNo' => $bike_info['imei'],
            'lockState'=> 0,
            'bikeState' => $bike_info['health_state'],
            'enableFlag' => $enableFlag,
            'address' => $bike_info['address'],
            'coordinateX' => $cords['x'],
            'coordinateY' => $cords['y'],
            'licenseNo' => "",
            'certificateNo' => "",
            'openTime' => $now,
            'updateTime' => $now,
            'recordTime' => $now,
            'bikeType' => 2,
        ];

        $postdata = [
            'data' => $data,
            'operate' => $operate,
        ];

        $result = common::curlpostjson($url, $postdata);
        //hilog::info($result);
        if (empty($result)) {
            return [-1, "request error!", $vt->cost()];
        }

        if ($result['success'] == true) {
            return [$result['code'], $result['message'], $vt->cost()];
        }

        return [-2, $result['message'], $vt->cost()];
    }


    //bike_postInfo
    function bike_postInfo($bike_info,$lock=0)
    {

        $vt = new vtimer();
        $cords = (new BaiDuMapModel())->wgs_to_baidu($bike_info['coordinate']);

        $now = date('Y-m-d H:i:s');

        $url = 'http://221.7.240.130:8027/HSGPS/api/v1/bike/posInfo';
        $data=[
            'unitID' => $this->agent_id,
            'unitName' => $this->agent_name,
            'bikeNo' => $bike_info['sn'],
            'lockNo' => $bike_info['imei'],
            'lockState'=> $lock,
            'bikeState' => $bike_info['health_state'],
            'enableFlag' => 1,
            'address' => $bike_info['address'],
            'coordinateX' => $cords['x'],
            'coordinateY' => $cords['y'],
            'licenseNo' => "",
            'certificateNo' => "",
            'openTime' => $now,
            'closeTime' => $now,
            'recordTime' => $now
        ];


        $postdata = [
            'data' => $data
        ];

        $result = common::curlpostjson($url, $postdata);
        if (empty($result)) {
            return [-1, "request error!", $vt->cost()];
        }

        if ($result['success'] == true) {
            return [$result['code'], $result['message'], $vt->cost()];
        }

        return [-2, $result['message'], $vt->cost()];
    }

    //bike_humaninfo
    function bike_humaninfo($staff_info,$operate)
    {

        $vt = new vtimer();
        $hs = [
            'Content-Type: application/json',
        ];

        $url = 'http://221.7.240.130:8027/HSGPS/api/v1/bike/humaninfo';
        $data=[
            'enterpriseName' => $this->agent_name,
            'humanName' => $staff_info['name'],
            'humanNo' => $staff_info['id'],
            'contact'=> $staff_info['phone'],
            'updateTime' => date('Y-m-d H:i:s'),
        ];

        $postdata = [
            'data' => $data,
            'operate' => $operate,
        ];

        $result = common::curlpostjson_timeout($url, $postdata,10, $hs);
        if (empty($result)) {
            return [-1, "request error!", $vt->cost()];
        }

        if ($result['success'] == true) {
            return [$result['code'], $result['message'], $vt->cost()];
        }

        return [-2, $result['message'], $vt->cost()];
    }
}
